{% extends "@CoreBundle/layout/main.html.twig" %}
{% import "@EduxBundle/macros/chapter_tree.html.twig" as macros %}

{% block body_custom_javascript %}
    <script>
        $(function(){
            $('#tree').nestable({maxDepth:3, scroll:true});
            $('.dd').nestable('collapseAll');
            
            $('.tree-tools').on('click', function(e){
                var action = $(this).data('action');
                if (action === 'expand') {
                    $('.dd').nestable('expandAll');
                }
                if (action === 'collapse') {
                    $('.dd').nestable('collapseAll');
                }
            });

            $("#sortSave").click(function(){
                var serialize = $('#tree').nestable('serialize');
                requestPost('{{ path('admin_api_teach_chapter_updateSort', {"id":id}) }}', serialize);
            });
        });

    </script>
{% endblock %}

{% block page %}
<div class="card card-default">
    <div class="card-header">
        <div class="btn-group">
            <button class="btn btn-primary btn-sm tree-tools" data-action="expand" title="展开">
                <i class="mdi mdi-plus-box-outline"></i>&nbsp;展开
            </button>
            <button class="btn btn-primary btn-sm tree-tools" data-action="collapse"
               title="收起">
                <i class="mdi mdi-minus-box-outline"></i>&nbsp;收起
            </button>
        </div>
        {% if is_authorized('admin_api_teach_chapter_updateSort') %}
            <div class="btn-group">
                <button class="btn btn-info btn-sm" id="sortSave" title="保存">
                    <i class="mdi mdi-content-save"></i><span class="hidden-xs">&nbsp;保存排序 </span>
                </button>
            </div>
        {% endif %}

        {% if is_authorized('admin_teach_chapter_add') %}
        <div class="btn-group">
            <a href="{{ path("admin_teach_chapter_add", {"id":id}) }}" class="btn btn-sm btn-success poppage" title="添加" style="margin-right: 10px;">
                <i class="mdi mdi-plus"></i><span class="hidden-sm">添加</span>
            </a>
        </div>
        {% endif %}

    </div>
    <!-- /.box-header -->
    <div class="card-body table-responsive">
        <div class="dd" id="tree">
            {{ macros.chapter_tree(all, 0) }}
        </div>
    </div>
    <!-- /.box-body -->
</div>

{% endblock %}

